package org.erp.admins.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.admins.infrastructure.convert.SalesChannelsConvert;
import org.erp.admins.domain.entity.SalesChannels;
import org.erp.admins.domain.dto.SalesChannelsDto;
import org.erp.admins.domain.vo.SalesChannelsVo;
import org.erp.admins.infrastructure.mapper.SalesChannelsMapper;
import org.erp.admins.service.SalesChannelsService;
import org.erp.common.page.PageQueryModel;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("salesChannelsService")
public class SalesChannelsServiceImpl extends ServiceImpl<SalesChannelsMapper, SalesChannels> implements SalesChannelsService {

    @Resource
    private SalesChannelsConvert salesChannelsConvert;

    @Override
    public PageQueryModel<SalesChannelsVo> getListByPage(SalesChannelsDto dto) {
        LambdaQueryWrapper<SalesChannels> wrapper = buildListQuery(dto);
        Page<SalesChannels> page = this.page(dto.buildPage(), wrapper);
        List<SalesChannelsVo> voList = page.getRecords().stream()
                .map(salesChannelsConvert::entityToVo).collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean updSalesChannels(SalesChannelsDto dto) {
        Assert.notNull(dto.getId(), "ID不能为空");
        Assert.isTrue(StringUtils.hasText(dto.getChannelName()), "渠道名称不能为空");
        Assert.isTrue(!existsByName(dto.getChannelName(), dto.getId()), "渠道名称已存在");
        SalesChannels entity = salesChannelsConvert.dtoToEntity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean addSalesChannels(SalesChannelsDto dto) {
        Assert.isTrue(StringUtils.hasText(dto.getChannelName()), "渠道名称不能为空");
        Assert.isTrue(!existsByName(dto.getChannelName(), null), "渠道名称已存在");
        SalesChannels entity = salesChannelsConvert.dtoToEntity(dto);
        entity.setDelFlag(0);
        return this.save(entity);
    }

    @Override
    public SalesChannelsVo getSalesChannelsVoById(Long id) {
        SalesChannels entity = this.getById(id);
        if (entity != null) {
            return salesChannelsConvert.entityToVo(entity);
        }
        return null;
    }

    @Override
    public List<SalesChannelsVo> getSalesChannelsVoList(SalesChannelsDto dto) {
        LambdaQueryWrapper<SalesChannels> wrapper = buildListQuery(dto);
        List<SalesChannels> list = this.list(wrapper);
        return list.stream().map(salesChannelsConvert::entityToVo).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<SalesChannels> buildListQuery(SalesChannelsDto dto) {
        LambdaQueryWrapper<SalesChannels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(dto.getChannelName()), SalesChannels::getChannelName, dto.getChannelName())
                .eq(SalesChannels::getDelFlag, 0)
                .orderByDesc(SalesChannels::getCreateTime);
        if (StringUtils.hasText(dto.getStatus())) {
            wrapper.eq(SalesChannels::getStatus, dto.getStatus());
        }
        return wrapper;
    }

    private boolean existsByName(String channelName, Long excludeId) {
        LambdaQueryWrapper<SalesChannels> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SalesChannels::getChannelName, channelName)
                .eq(SalesChannels::getDelFlag, 0);
        if (excludeId != null) {
            wrapper.ne(SalesChannels::getId, excludeId);
        }
        return this.count(wrapper) > 0;
    }
}

